package davion.study.summercloud.service;

import davion.study.summercloud.mapper.FileFolderMapper;
import davion.study.summercloud.mapper.FileStoreMapper;
import davion.study.summercloud.mapper.MyFileMapper;
import davion.study.summercloud.model.*;
import davion.study.summercloud.util.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author: Davion
 * @date: 2020/6/16
 * @description:
 */
@Service
public class FileFolderService {
    @Resource
    FileFolderMapper fileFolderMapper;

    @Resource
    FileStoreMapper fileStoreMapper;

    @Resource
    MyFileMapper myFileMapper;

    @Value("${ftp.url}")
    private String ftpURL;

    @Value("${ftp.username}")
    private String ftpUsername;

    @Value("${ftp.password}")
    private String ftpPassword;

    FtpUtil ftpUtil = null;

    public List<FileFolder> getRootFoldersByUserId(Integer userId) {
        FileStoreExample storeExample = new FileStoreExample();
        storeExample.createCriteria()
                .andUserIdEqualTo(userId);
        List<FileStore> fileStores = fileStoreMapper.selectByExample(storeExample);
        if (fileStores.size() == 0) {
            return null;
        }
        FileFolderExample folderExample = new FileFolderExample();
        folderExample.createCriteria()
                .andStoreIdEqualTo(fileStores.get(0).getId())
                .andParentIdEqualTo(0);
        List<FileFolder> folders = fileFolderMapper.selectByExample(folderExample);
        return folders;
    }


    public List<FileFolder> getFoldersByParentId(Integer parentId) {
        FileFolderExample folderExample = new FileFolderExample();
        folderExample.createCriteria()
                .andParentIdEqualTo(parentId);
        List<FileFolder> folders = fileFolderMapper.selectByExample(folderExample);
        return folders;
    }

    public FileFolder getFoldersById(Integer folderId) {
        return fileFolderMapper.selectByPrimaryKey(folderId);
    }

    public List<FileFolder> getRootFoldersByParentId(Integer storeId) {
        FileFolderExample folderExample = new FileFolderExample();
        folderExample.createCriteria()
                .andStoreIdEqualTo(storeId)
                .andParentIdEqualTo(0);
        return fileFolderMapper.selectByExample(folderExample);
    }

    public Boolean addFolder(FileFolder folder) {
        return fileFolderMapper.insert(folder) == 1;
    }

    public Boolean updateFolder(FileFolder dbFolder) {
        return fileFolderMapper.updateByPrimaryKey(dbFolder) == 1;
    }

    public void deleteFolder(FileFolder folder) throws IOException {
        // 删除当前文件夹下所有文件
        MyFileExample fileExample = new MyFileExample();
        fileExample.createCriteria()
                .andFolderIdEqualTo(folder.getId());
        List<MyFile> dbFiles = myFileMapper.selectByExample(fileExample);
        FtpUtil ftpUtil = FtpUtil.createFtpCli(ftpURL, ftpUsername, ftpPassword, "/");
        ftpUtil.connect();
        for (MyFile dbFile : dbFiles) {
            ftpUtil.deleteFile(dbFile.getFilePath() + "/" + dbFile.getName() + dbFile.getPostfix());
            myFileMapper.deleteByPrimaryKey(dbFile.getId());
            // 更新文件仓库容量
            FileStore fileStore = fileStoreMapper.selectByPrimaryKey(dbFile.getStoreId());
            fileStore.setCurrentSize(fileStore.getCurrentSize() - dbFile.getSize());
            fileStoreMapper.updateByPrimaryKey(fileStore);
        }
        ftpUtil.disconnect();

        // 递归删除子文件夹
        FileFolderExample folderExample = new FileFolderExample();
        folderExample.createCriteria()
                .andParentIdEqualTo(folder.getId());
        List<FileFolder> dbFolders = fileFolderMapper.selectByExample(folderExample);
        for (FileFolder dbFolder : dbFolders) {
            deleteFolder(dbFolder);
        }
        fileFolderMapper.deleteByPrimaryKey(folder.getId());
    }
}
